﻿// toast
var shengyu = 0;
$(function () {

    var $toast = $('#js_toast');
    var $input = $('#js_input_xm'); 
    var $input_dhhm = $('#js_input_dhhm');

    $input.on('input', function () {
        if ($input.val()) {
            $('#showTooltips').removeClass('weui-btn_disabled');
        } else {
            $('#showTooltips').addClass('weui-btn_disabled');
        }
    });

    $input_dhhm.on('input', function () {
        if ($input_dhhm.val().length > 11) {
            $input_dhhm.val($input_dhhm.val().slice(0, 11));
        }
    });

    //保存预定信息
    $('#showTooltips').on('click', function () {

        SendOrder();

      

    });

    //获取所有的药店
    getBaseInfo();

    //设置最大的购买数目
    //setMax_Kouzhao(3, 0);
    getKouZhaoPram();

    //显示二维码界面
    $('#show_order').click(function () {
        //window.location.href = './yuding_show_QR.html';
        window.location.href = "http://" + A_Url + '/Main/weixin/guke_order_find.html';
    });

    ////获取验证码
    getcode();
    ////获取预约时段
    getyiyueshiduan();
});

var random='';
function getcode() {
    $("#code").attr("src", "http://" + A_Url + "/ImgCode/MixVerifyCode?code=" + rand(1000, 9999));
}

function rand(min, max) {
    var rd = Math.floor(Math.random() * (max - min)) + min;
    random = rd;
    return rd;
}

//获取预约时段
function getyiyueshiduan() {
    $.ajax({
        type: "GET",
        url: "http://" + A_Url + "/api/baseInfo?info=yiyueshiduan&xianid=" + Xian_ID,
        //数据，json字符串
        //data: JSON.stringify(list),
        success: function (result) {
            $('#yiyueshiduan').text(result);
        },
        error: function (e) { }
    });

}

//获取口罩参数
function getKouZhaoPram() {
    $.ajax({
        type: "GET",
        url: "http://" + A_Url + "/api/baseInfo?info=kouzhaoshu",
        //数据，json字符串
        //data: JSON.stringify(list),
        success: function (result) {
            setMax_Kouzhao(result.ParamValue, 1);
        },
        error: function (e) { }
    });

}

function setMax_Kouzhao(MAX = 3, MIN = 1) {
    //var MAX = 3, MIN = 1;
    $('#js_input_sl').val(MAX);
    $('.weui-count__decrease').click(function (e) {
        var $input = $(e.currentTarget).parent().find('.weui-count__number');
        var number = parseInt($input.val() || "0") - 1;
        if (number < MIN) number = MIN;
        $input.val(number);
    });
    $('.weui-count__increase').click(function (e) {
        var $input = $(e.currentTarget).parent().find('.weui-count__number');
        var number = parseInt($input.val() || "0") + 1;
        if (number > MAX) number = MAX;
        $input.val(number);
    });
}


//获取所有的药店
var yaodian;
function getBaseInfo() {
    $.ajax({
        type: "GET",
        url: "http://" + A_Url + "/api/baseInfo?info=org",
        //数据，json字符串
        data: { xianid: Xian_ID},
        success: function (result) {
            if (result == undefined || result == null || result.length == 0) return;

            var rt_items = [];
            for (var a = 0; a < result.length; a++) {
                rt_items.push({ title: result[a].YaoDianMing + ':' + result[a].YaoDianDiZhi, value: result[a].ID, addr: result[a].YaoDianDiZhi, shengyu: result[a].shengyu});
            }
            
            $("#job").select({
                title: "请选择门店",

                items: rt_items,
                onChange: function (d) {
                    yaodian = d.values;
                    $('#ydaddr').val(d.origins[0].addr);
                    $('#js_input_kcsl').val(d.origins[0].shengyu);
                    shengyu = d.origins[0].shengyu;
                },
                onClose: function () {

                },
                onOpen: function () {

                }
            });
        },
        error: function (e) { }
    });

}

//发送当前预定信息
function SendOrder() {
    if (shengyu <= 0) {
        $.alert("库存不足！", "失败", function () {
        });
        return;
    }
    var redomcode = $('#redomcode').val();
    random += '';
    if (redomcode != random) {
        $.alert("验证码出错！", "失败", function () {
        });
        return;
    }

    //var yaodian = $('#job').val();
    var xm = $('#js_input_xm').val();
    var sfz =$('#js_input_sfz').val();
    var jtzz = '';//$('#js_input_jtzz').val();
    var wx = '';
    try {
        var fingerprint1 = new Fingerprint({ screen_resolution: true }).get();
        wx = fingerprint1;
    }
    catch (err) { }

    var sl =$('#js_input_sl').val();
    var dhhm = $('#js_input_dhhm').val();

    if (yaodian == undefined || yaodian == null || yaodian == '') {
        $.alert("请选择药店！", "信息不全", function () {
        });
        return;
    }
    if (xm == undefined || xm == null || xm == '') {
        $.alert("请输入姓名！", "信息不全", function () {
        });
        return;
    }
    if (sfz == undefined || sfz == null || sfz == '') {
        $.alert("请输入身份证！", "信息不全", function () {
        });
        return;
    }
    //if (jtzz == undefined || jtzz == null || jtzz == '') {
    //    $.alert("请输入家庭住址！", "信息不全", function () {
    //    });
    //    return;
    //}
    if (sl == undefined || sl == null || sl == 0 ) {
        $.alert("数量不正确！", "信息不全", function () {
        });
        return;
    }
    if (dhhm == undefined || dhhm == null || dhhm == '') {
        $.alert("请输入电话号码！", "信息不全", function () {
        });
        return;
    }

    if (!checkPhone(dhhm)) {
        $.alert("手机号证校验失败！", "信息不全", function () {
        });
        return;
    }

    if (!checkIdCard(sfz)) {
        $.alert("身份证校验失败！", "信息不全", function () {
        });
        return;
    }

    $.showLoading();
    var tm = setTimeout(function () {
        $.hideLoading();
        $.alert("系统繁忙，请稍后再试！", "超时", function () { });
    }, 20000);

    $.ajax({
        type: "GET",
        url: "http://" + A_Url + "/api/yudingsave",
        //数据，json字符串
        data: { info: 'saveorder', online: '0', yaodian: yaodian, xm: xm, sfz: sfz, jtzz: jtzz, sl: sl, dhhm: dhhm, wx: wx, xianid: Xian_ID },
        success: function (result) {
            $.hideLoading();
            clearTimeout(tm);
            if (result == undefined || result == null || result == -1 || result == 0) {
                $.alert("", "预定失败", function () {
                    return;
                });
            } else {
                var msg = "预定成功！";
                if (result == -5) {
                    msg = '不能重复预定！';
                } else if (result == -2) {
                    msg = '库存不足！';
                } else if (result == -999) {
                    msg = '预定出错！';
                } else if (result == -666) {
                    msg = '未到购买时间！';
                }
                $.alert("", msg, function () {
                    if (result > 0) {
                        var date = new Date();
                        //date.setDate(date.getDate() + 1);
                        //var dt = getDateNumber(date);
                        //getDateNumber(date);
                        window.location.href = "http://" + A_Url + '/Main/weixin/guke_order_QR.html?selectdate=' + getDateNumber(date) + '&orderid=' + result;
                    }
                });
            }
        },
        error: function (e) { }
    });


}
